// 在文件开头添加
const CONTEXT_PATH = window.location.pathname.substring(0, window.location.pathname.indexOf('/', 1));

// 通用工具函数
const utils = {
    // 格式化日期
    formatDate: function(timestamp) {
        if (!timestamp) return '-';
        const date = new Date(timestamp);
        return date.getFullYear() + '-' + 
               this.padZero(date.getMonth() + 1) + '-' + 
               this.padZero(date.getDate()) + ' ' + 
               this.padZero(date.getHours()) + ':' + 
               this.padZero(date.getMinutes());
    },

    // 数字补零
    padZero: function(num) {
        return num < 10 ? '0' + num : num;
    },

    // 获取难度标签
    getDifficultyLabel: function(difficulty) {
        switch(difficulty) {
            case 1: return '<span class="badge bg-success">简单</span>';
            case 2: return '<span class="badge bg-warning">中等</span>';
            case 3: return '<span class="badge bg-danger">困难</span>';
            default: return '<span class="badge bg-secondary">未知</span>';
        }
    },

    // 生成分页HTML
    generatePagination: function(currentPage, totalPages, onPageChange) {
        let html = '';
        if (totalPages > 1) {
            // 上一页
            html += `<li class="page-item ${currentPage <= 1 ? 'disabled' : ''}">
                        <a class="page-link" href="#" data-page="${currentPage - 1}">上一页</a>
                    </li>`;

            // 页码
            for (let i = 1; i <= totalPages; i++) {
                if (i === currentPage) {
                    html += `<li class="page-item active">
                                <span class="page-link">${i}</span>
                            </li>`;
                } else {
                    html += `<li class="page-item">
                                <a class="page-link" href="#" data-page="${i}">${i}</a>
                            </li>`;
                }
            }

            // 下一页
            html += `<li class="page-item ${currentPage >= totalPages ? 'disabled' : ''}">
                        <a class="page-link" href="#" data-page="${currentPage + 1}">下一页</a>
                    </li>`;
        }
        return html;
    }
};

// 通用事件处理
$(document).ready(function() {
    // 处理所有ajax请求的错误
    $(document).ajaxError(function(event, xhr, settings) {
        if (xhr.status === 401) {
            alert('请先登录');
            window.location.href = CONTEXT_PATH + '/user/login';
        } else if (xhr.status === 403) {
            alert('没有权限执行此操作');
        } else {
            alert('操作失败，请稍后重试');
        }
    });

    // 处理所有的删除确认
    $(document).on('click', '[data-confirm]', function(e) {
        if (!confirm($(this).data('confirm'))) {
            e.preventDefault();
            return false;
        }
    });
});

// 添加自定义样式
const style = document.createElement('style');
style.textContent = `
    .badge {
        font-size: 0.875rem;
    }
    .table > tbody > tr > td {
        vertical-align: middle;
    }
    .btn-sm {
        padding: 0.25rem 0.5rem;
        font-size: 0.875rem;
    }
    .nav-link.active {
        font-weight: bold;
    }
`;
document.head.appendChild(style); 